using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Storage
{
    [Authorize]
    public class WarehouseAppService : BasicArchivesAppService, IWarehouseAppService
    {
        protected IWarehouseRepository WarehouseRepository => LazyServiceProvider.LazyGetRequiredService<IWarehouseRepository>();
        protected WarehouseManager WarehouseManager => LazyServiceProvider.LazyGetRequiredService<WarehouseManager>();

        [Authorize(BasicArchivesPermissions.Warehouse.Query)]
        public async Task<WarehouseDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<Warehouse, WarehouseDto>(
                await WarehouseRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Query)]
        public async Task<WarehouseDto> FindAsync(string filter)
        {
            var entity = await WarehouseRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<Warehouse, WarehouseDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Query)]
        public async Task<WarehouseDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await WarehouseRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<Warehouse, WarehouseDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Query)]
        public async Task<PagedResultDto<WarehouseDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await WarehouseRepository.GetCountAsync(input.Filter, true);
            var list = await WarehouseRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<WarehouseDto>(
                totalCount,
                ObjectMapper.Map<List<Warehouse>, List<WarehouseDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Query)]
        public async Task<PagedResultDto<WarehouseDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<Warehouse, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<Warehouse>(input.FilterGroup);
            }

            var totalCount = await WarehouseRepository.GetCountAsync(expression, true);
            var list = await WarehouseRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<WarehouseDto>(
                totalCount,
                ObjectMapper.Map<List<Warehouse>, List<WarehouseDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Create)]
        public async Task<WarehouseDto> CreateAsync(WarehouseCreateDto input)
        {
            var entity = ObjectMapper.Map<WarehouseCreateDto, Warehouse>(input);

            await WarehouseManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Update)]
        public async Task<WarehouseDto> UpdateAsync(Guid id, WarehouseUpdateDto input)
        {
            var entity = await WarehouseRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await WarehouseManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.Warehouse.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await WarehouseManager.DeleteAsync(id);
        }
    }
}